"""
shopping_process_page.py
    1.对购物流程进行封装
    2.封装页面表现层和操作层
    3.继承base.py中的Base类
"""
from common.Base import Base, open_browser
from selenium.webdriver.common.by import By
from Page.shoppingcart_page import Shopingcart_page

url = "http://localhost:8080/ecshop/user.php"


class shopping_process(Base):
    '''登录'''
    username_loc = (By.NAME, "username")  # 用户名输入框
    password_loc = (By.NAME, "password")  # 密码输入框
    submit_loc = (By.CLASS_NAME, "us_Submit")  # 立即登录按钮
    homepage_loc = (By.LINK_TEXT, "首页")  # 首页链接
    """商品元素定位家用电器栏"""
    # 智能相机
    camera1_loc = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(1) .goodsimg')
    """商品元素详情加入购物车按钮定位"""
    # 商品加入购物车按钮
    goods_confirm_loc = (By.CSS_SELECTOR, '.td1 img')
    # 结算按钮
    goods_settlement_loc = (By.CSS_SELECTOR, 'body > div:nth-child(7) > div.flowBox > table > tbody'
                                             ' > tr > td:nth-child(2) > a > img')
    # 提交订单按钮
    goods_submit_loc = (By.CSS_SELECTOR, "#theForm > div:nth-child(16) >"
                                         " div:nth-child(3) > input[type=image]:nth-child(1)")
    """商品元素定位1f家用电器栏"""
    # 智能相机
    Smartcamera_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(1) .goodsimg')
    # 炫彩翻页保护套
    Colorfulflipcover_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(2) .goodsimg')
    # 平衡车
    Balanceofthecar_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(3) .goodsimg')
    # 透明超薄软胶保护套
    superProtectivesleeve_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(4) .goodsimg')
    # 运动相机
    Sportscamera_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(5) .goodsimg')
    # 自拍杆
    Sincetheshaft_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(6) .goodsimg')
    # 随身风扇
    Keepthefan_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(7) .goodsimg')
    # 视频
    video_location = (By.CSS_SELECTOR, '.clearfix:nth-child(2) .goodsItem:nth-child(8) .goodsimg')

    """购物车内部元素定位"""
    # 商品数量输入框
    goods_number_loc = (By.XPATH, '//*[@id="formCart"]/table/tbody/tr[2]/td[5]/input')
    # 更新购物车按钮
    refresh_goods_loc = (By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[2]')
    # 清空购物车按钮
    clear_goods_loc = (By.XPATH, '//*[@id="formCart"]/table[2]/tbody/tr/td[2]/input[1]')
    # 继续购物按钮
    continue_shopping_loc = (By.XPATH, '/html/body/div[6]/div[1]/table/tbody/tr/td[1]/a/img')

    # 删除购物车第一位商品
    del_good_loc = (By.CSS_SELECTOR, "tr:nth-child(2)>td:nth-child(7)>.f6")
    # 获取购物车金额
    shoppingcart_money_loc = (By.XPATH, "//form[@id = 'formCart']/table[2]/tbody/tr/td")

    # 定位购物车
    shopping_loc = (By.XPATH, "//a[@title = '查看购物车']")
    # 定位购物车中的商品数量
    judge_number_loc = (By.PARTIAL_LINK_TEXT, "购物")
    # 定位商品名称
    shopping_car_good_loc = (By.XPATH, "//*[@id='formCart']/table/tbody/tr[2]/td/a[2]")

    # 商品分类定位
    goodsclass_loc = (By.XPATH, "//*[@id = mainNav]/div/ul/li[2]/a")
    # 定位商品分类中第一个商品
    goodsclass_thegood_loc = (By.XPATH, "//*[@name='compareForm']/div/div/div/a/img")

    def click_superProtectivesleeve(self):
        """
        点击透明超薄软胶保护套商品
        :return:
        """
        self.click(self.superProtectivesleeve_location)

    def click_Sincetheshaft(self):
        """
        点击自拍杆商品
        :return:
        """
        self.click(self.Sincetheshaft_location)

    def cilck_Keepthefan(self):
        """
        点击随身风扇商品
        :return:
        """
        self.click(self.Keepthefan_location)

    def click_video(self):
        """
        点击视频商品
        :return:
        """
        self.click(self.video_location)

    def click_Smartcamera(self):
        """
        点击智能相机商品
        :return:
        """
        self.click(self.Smartcamera_location)

    def click_Sportscamera(self):
        """
        点击运动相机商品
        :return:
        """
        self.click(self.Sportscamera_location)

    def click_Balanceofthecar(self):
        """
        点击平衡车商品
        :return:
        """
        self.click(self.Balanceofthecar_location)

    def click_Colorfulflipcover(self):
        """
        点击炫彩翻页保护套商品
        :return:
        """
        self.click(self.Colorfulflipcover_location)

    def click_goods_confirm(self):
        """
        点击加入购物车按钮
        :return:
        """
        self.click(self.goods_confirm_loc)

    def click_refresh_goods(self):
        """
        点击更新购物车按钮
        :return:
        """
        self.click(self.refresh_goods_loc)

    def click_continue_shopping(self):
        """
        点击继续购物按钮
        :return:
        """
        self.click(self.continue_shopping_loc)

    def click_clear_goods(self):
        """
        点击清空购物车按钮
        :return:
        """
        self.click(self.clear_goods_loc)

    def in_shopping_cart(self):
        '''
        定位到购物车该商品的
        :return:
        '''
        return self.get_element_text(self.shopping_car_good_loc)

    def input_username(self, username):
        """
        输入用户名
        :param username:
        :return:
        """
        self.send_text(self.username_loc, username)

    def input_password(self, password):
        """
        输入密码
        :param password:
        :return:
        """
        self.send_text(self.password_loc, password)

    def click_submit(self):
        """
        点击立即登录
        :return:
        """
        self.click(self.submit_loc)

    def click_homepage(self):
        """
        点击首页
        :return:
        """
        self.click(self.homepage_loc)

    def click_camera1(self):
        """
        点击智能相机
        :return:
        """
        self.click(self.camera1_loc)

        self.click(self.goods_confirm_loc)

    def click_goods_settlement(self):
        """
        点击结算按钮
        :return:
        """
        self.click(self.goods_settlement_loc)

    def click_goods_submit(self):
        """
        点击提交订单
        :return:
        """
        self.click(self.goods_submit_loc)

    def click_del_good(self):
        '''
        点击删除
        :return:
        '''
        self.click(self.del_good_loc)

    def git_window_text(self):
        '''
        获取弹框文本
        :return: 
        '''
        alt = self.driver.switch_to.alert.text
        return alt

    def click_window(self):
        '''
        获取点击弹框确定
        :return:
        '''
        self.driver.switch_to.alert.accept()

    def git_shoppingcart_money(self):
        '''
        获取购物车金额
        :return:
        '''
        return self.get_element_text(self.shoppingcart_money_loc)

    def click_shoppingcar(self):
        '''
        点击右上角购物车
        :return:
        '''
        self.click(shopping_process.shopping_loc)

    def update_goods_number(self, number):
        '''
        购物车内 视频商品输入框修改值
        :return:
        '''

        # 清空输入框
        self.clear_send_text(self.goods_number_loc)
        # 输入值
        self.send_text(self.goods_number_loc, number)

    def goods_number(self):
        '''
        购物车内的商品数量
        :return:
        '''
        return self.get_value(self.goods_number_loc, )

    def shoppingcart_judge_number(self, text):
        '''
        判断购物车数量是否符合预期
        :return:T/F
        '''
        return self.is_text_in_element(self.judge_number_loc, text=text)

    def click_goodsclass(self):
        '''
        定位商品分类 第一个
        :return:
        '''
        self.click((By.LINK_TEXT, "服装"))

    def click_goodsclass_thegood(self):
        '''
        定位商品分类中的商品
        :return:

        '''
        self.click(self.goodsclass_thegood_loc)

    def click_imageField(self):
        '''
        定位搜索按钮并点击
        :return:
        '''
        self.click((By.NAME, "imageField"))

    def click_imageField_good(self):
        '''
        定位搜索后的商品
        :return:
        '''
        self.click((By.LINK_TEXT, "视频"))

    def click_add_favorites(self):
        '''
        购物车页面
        放入收藏夹
        :return: 
        '''
        self.click((By.LINK_TEXT, "放入收藏夹"))

    def click_add_the_favorites(self):
        '''
        商品页面
        加入收藏夹
        :return:
        '''
        self.click((By.XPATH, "/html/body/div[4]/div[3]/div/div[1]/div[2]/form/ul/li[7]/table/tbody/tr/td[2]/a/img"))

    def click_usercenter(self):
        '''
        点击进入用户中心
        :return:
        '''
        self.click((By.LINK_TEXT, "用户中心"))

    def click_mycollection(self):
        '''
        点击我的收藏
        :return:
        '''
        self.click((By.LINK_TEXT, "我的收藏"))

    def is_mycollection(self):
        """
        获取我的收藏夹
        :return:
        """
        return self.get_element_text((By.CSS_SELECTOR, "h5:nth-child(1)>span"))

    def click_add_shoppingcart(self):
        '''
        定位收藏夹页面加入购物车按钮
        :return:
        '''
        self.click((By.LINK_TEXT, "加入购物车"))

    def click_del(self):
        '''
        点击删除
        :return:
        '''
        self.click((By.LINK_TEXT, "删除"))

    def select_goods(self, goods):
        '''
        查找商品
        :return:
        '''
        return self.get_element_text((By.LINK_TEXT, goods))

    def update_addr(self):
        '''
        点击修改收货地址
        :return:
        '''
        self.get_element_text((By.CSS_SELECTOR, ".flowBox:nth-child(4) .f6"))

    def click_invoicing(self):
        '''
        点击确定结算
        :return:
        '''
        self.click((By.XPATH, "/html/body/div[6]/form/div[15]/div[2]/input[1]"))

    def click_distribution_1(self):
        '''
        点击第一个配送方式
        :return:
        '''
        self.click((By.XPATH, "/html/body/div[6]/form/div[5]/table/tbody/tr[2]/td[1]/input"))

    def click_distribution_2(self):
        '''
        点击第二个配送方式
        :return:
        '''
        self.click((By.XPATH, "/html/body/div[6]/form/div[5]/table/tbody/tr[3]/td[1]/input"))

    def click_payment1(self):
        '''
        点击第一个付款方式
        :return:
        '''
        self.click((By.XPATH, "/html/body/div[6]/form/div[7]/table/tbody/tr[2]/td[1]/input"))

    def click_payment2(self):
        '''
        点击第二个付款方式
        :return:
        '''
        self.click((By.XPATH, "/html/body/div[6]/form/div[7]/table/tbody/tr[3]/td[1]/input"))

    def get_order_number(self):
        '''
        结算后获取订单号
        :return:
        '''
        return self.get_element_text((By.XPATH, "/html/body/div[6]/div/h6/font"))

    def click_myorder(self):
        '''
        点击我的订单
        :return:
        '''
        self.click((By.XPATH, "/html/body/div[6]/div[1]/div/div/div/div/a[3]"))

    def get_myorder(self):
        '''
        获取我的订单
        :return:
        '''
        return self.get_element_text((By.XPATH, "/html/body/div[6]/div[2]/div/div/div/table/tbody/tr[1]/td[1]/a"))


# 调试
if __name__ == '__main__':
    # 实例化浏览器 进入网页
    op = open_browser()
    shop = shopping_process(op)
    shop.open_url("http://localhost:8080/ecshop")
    shop.click_usercenter()
    shop.click_mycollection()

    # # 点击视频商品
    # shop.click_video()
    # # 点击加入购物车
    # shop.click_goods_confirm()
    # print(shop.in_shopping_cart())

    # shop.click_imageField()
    # shop.click_imageField_good()
    # shop.click_goods_confirm()

    # shop.click_goodsclass()
    # shop.click_goodsclass_thegood()
    # shop.click_goods_confirm()

    # # 点击视频商品
    # shop.click_video()
    # # 点击加入购物车
    # shop.click_goods_confirm()
    # shop.update_goods_number("4")
    # # 点击更新购物车
    # shop.click_refresh_goods()
    # # 点击进入购物车
    # shop.click_shoppingcar()
    # return self..text

    # # 点击继续购物
    # shop.click_continue_shopping()
    # # 点击点击炫彩翻页保护套商品()
    # shop.click_Colorfulflipcover()
    # # 点击加入购物车
    # shop.click_goods_confirm()
    # print(shop.shoppingcart_judge_number("购物车(2)"))
    # shop.quit()

    # shop.input_username("111")
    # shop.input_password("111111")
    # shop.click_submit()
    # shop.click_homepage()
    # shop.click_Smartcamera()
    # shop.click_goods_confirm()
    #
    # op.execute_script("arguments[0].value='2'", shop.Shoping_number())
    # shop.Shoping_number("3")
    # del_good_loc = (By.CSS_SELECTOR,"tr:nth-child(2)>td:nth-child(7)>.f6")
    # shop.click_del_good()
    # shop.git_window_text()
    # shop.click_window()
    # print(shop.git_shoppingcart_money())
